package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.core.Result;
import com.campsys.core.TableData;
import com.campsys.domain.TActivityCategory;
import com.campsys.domain.TManagerActivity;
import com.campsys.domain.param.*;
import com.campsys.domain.vo.ActivityCategoryListVo;
import com.campsys.domain.vo.ActivityCategoryPageVo;
import com.campsys.domain.vo.ActivityListVo;
import com.campsys.domain.vo.ActivityPageVo;
import com.campsys.mapper.UserMapper;
import com.campsys.mapping.ActivityMapping;
import com.campsys.service.IActivityCategoryService;
import com.campsys.service.IActivityService;
import com.campsys.util.JWTUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "活动管理")
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityCategoryService activityCategoryService;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation("获取活动类型分页列表")
    @PreAuthorize("@lcc.myAuthority('activity:category:page')")
    @GetMapping("category/page")
    public TableData<ActivityCategoryPageVo> getActivityCategoryPage(@Validated PageParam pageParam){

        Page<TActivityCategory> activityCategoryPage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());

        Page<TActivityCategory> page = activityCategoryService.page(activityCategoryPage);

        List<ActivityCategoryPageVo> list = ActivityMapping.INSTANCE.toPage(page.getRecords());

        return TableData.success(list,page.getTotal());
    }

    @ApiOperation("获取活动类型列表")
    @PreAuthorize("@lcc.myAuthority('activity:category:list')")
    @GetMapping("category/list")
    public Result<List<ActivityCategoryListVo>> getActivityCategoryList(){

        List<ActivityCategoryListVo> list = ActivityMapping.INSTANCE.toList(activityCategoryService.list());

        return Result.success(list);
    }

    @ApiOperation("添加活动类型")
    @PreAuthorize("@lcc.myAuthority('activity:category:add')")
    @PostMapping("category/add")
    public Result addActivityCategory(@Validated @RequestBody ActivityCategoryAddParam activityCategoryAddParam){

        boolean rs = activityCategoryService.save(ActivityMapping.INSTANCE.to(activityCategoryAddParam));

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("删除活动类型")
    @PreAuthorize("@lcc.myAuthority('activity:category:delete')")
    @DeleteMapping("category/delete/{id}")
    public Result deleteActivityCategory(@PathVariable Long id){

        boolean rs = activityCategoryService.removeById(id);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("修改活动类型")
    @PreAuthorize("@lcc.myAuthority('activity:category:update')")
    @PutMapping("category/update")
    public Result updateActivityCategory(@Validated @RequestBody ActivityCategoryUpdateParam activityCategoryUpdateParam){

        boolean rs = activityCategoryService.updateById(ActivityMapping.INSTANCE.to(activityCategoryUpdateParam));

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("添加活动")
    @PreAuthorize("@lcc.myAuthority('activity:add')")
    @PostMapping("/add")
    public Result addActivity(@Validated @RequestBody ActivityAddParam activityAddParam, HttpServletRequest request){

        TManagerActivity activity = ActivityMapping.INSTANCE.to(activityAddParam);
        activity.setCreator(userMapper.getNameByUserId(JWTUtil.getUserId(request.getHeader("Authorization"))));
        activity.setStatus(0l);
        boolean rs = activityService.save(activity);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("修改活动")
    @PreAuthorize("@lcc.myAuthority('activity:update')")
    @PutMapping("/update")
    public Result updateActivity(@Validated @RequestBody ActivityUpdateParam activityUpdateParam){

        TManagerActivity activity = ActivityMapping.INSTANCE.to(activityUpdateParam);

        boolean rs = activityService.updateById(activity);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("查询活动分页列表")
    @PreAuthorize("@lcc.myAuthority('activity:page')")
    @GetMapping("/page")
    public TableData getActivityPage(PageParam pageParam, String name){

        LambdaQueryWrapper<TManagerActivity> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtil.isNullOrEmpty(name),TManagerActivity::getName,name);
        qw.ne(TManagerActivity::getStatus,3);

        Page<TManagerActivity> activityPage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());

        Page<TManagerActivity> page = activityService.page(activityPage,qw);

        List<ActivityPageVo> list = ActivityMapping.INSTANCE.toPageActivity(page.getRecords());

        return TableData.success(list,page.getTotal());
    }

    @ApiOperation("删除活动")
    @PreAuthorize("@lcc.myAuthority('activity:delete')")
    @PutMapping("/delete/{id}")
    public Result deleteActivity(@PathVariable Long id){

        TManagerActivity activity = new TManagerActivity();
        activity.setId(id);
        activity.setStatus(3l);

        boolean rs = activityService.updateById(activity);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("发布活动")
    @PreAuthorize("@lcc.myAuthority('activity:publish')")
    @PutMapping("/publish/{id}")
    public Result publishActivity(@PathVariable Long id){

        TManagerActivity activity = new TManagerActivity();
        activity.setId(id);
        activity.setStatus(1l);

        boolean rs = activityService.updateById(activity);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("关闭活动")
    @PreAuthorize("@lcc.myAuthority('activity:close')")
    @PutMapping("/close/{id}")
    public Result closeActivity(@PathVariable Long id){

        TManagerActivity activity = new TManagerActivity();
        activity.setId(id);
        activity.setStatus(2l);

        boolean rs = activityService.updateById(activity);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("查看活动详情")
    @PreAuthorize("@lcc.myAuthority('activity:detail')")
    @GetMapping("/detail/{id}")
    public Result detailActivity(@PathVariable Long id){

        TManagerActivity activity = activityService.getById(id);

        return activity != null ? Result.success(activity) : Result.error();
    }

    @ApiOperation("查询活动列表")
    @PreAuthorize("@lcc.myAuthority('activity:list')")
    @GetMapping("/list")
    public Result getActivityList(){

        LambdaQueryWrapper<TManagerActivity> qw = new LambdaQueryWrapper<>();
        qw.ne(TManagerActivity::getStatus,3);

        List<ActivityListVo> list = ActivityMapping.INSTANCE.toListActivity(activityService.list(qw));

        return Result.success(list);
    }

}
